package com.service.calculate.vehicle;

import com.service.bean.data.Data;
import com.service.bean.data.Data_1_x;
import com.service.bean.data.Data_2_x;
import com.service.bean.signal.GpsSignal;
import com.service.other.ServerConfig;

/**
 * 状态位计算
 * @author sxc
 *
 */
public class StatusCalculator {	
	
	public void calculate(Data data,GpsSignal signal){
		this.init(data);
		// 取反(该判断仅用于宜宾城管)
		if("009".equals(signal.companyCode)){
			this.ptoStatus = signal.equipmentType == 1 ? (this.ptoStatus == 1 ? 0 : 1) : this.ptoStatus;
		}
		signal.statusValue = this.rawStatus;
		signal.switchValue = this.rawSwitch;
		signal.hitchValue = this.rawFault;
		signal.outputValue = this.rawOutput;
		signal.accStatus = this.accStatus;//ACC状态
		signal.waterStatus = this.getWaterStatus();//加水状态
		signal.dumpStatus = this.isWasteDumping;//垃圾倾倒状态
		signal.sewageStatus = this.isSewageFull == 1 || this.isSewageEmission==1 ? 1:0 ;//污水状态
		signal.plateStatus = this.isBroomDown == 1 || this.isBroomDown2 == 1 ? 1 : 0; //扫盘状态
		signal.nozzleStatus = this.isNozzleDown;//吸嘴状态
		signal.wateringStatus = this.getWateringStatus();// 洒水状态
		if (signal.companyCode.equals(ServerConfig.instance().jstscode)) { // 048 江苏太仓公路处
			if (signal.equipmentType == 4) { //洗扫车
				signal.wateringStatus = this.leftWateringStatus;
			} else if(signal.equipmentType == 2 || signal.equipmentType == 5) { //洒水车 || 高压冲洗车
				signal.wateringStatus = (this.leftWateringStatus << 1) + this.rightWateringStatus;
			}
		}
		signal.binStatus = signal.equipmentType == 1 ? this.isTrashDown : 0;//垃圾箱状态
		signal.stuffStatus = signal.equipmentType == 3 ? this.isTrashDown : 0;//填装箱状态
		signal.strengthStatus = this.ptoStatus;//取力器状态
		signal.batteryStatus = this.batteryStatus;//电池状态
		signal.gpsStatus = this.gpsStatus;//定位状态
		signal.gpsAirStatus = this.gpsAerialStatus;//GPS天线状态
		signal.serviceStatus = this.arrearsStatus;//GPS服务状态
		signal.configStatus = this.settingStatus;//设置状态
		signal.outPut0 = this.output0;//输出状态1
		signal.outPut1 = this.output1;//输出状态2
		//signal.lockStatus;//锁车状态
		//signal.isInWork;//工作状态
		signal.directionSN = this.directionSN;
		signal.directionEW = this.directionEW;
		signal.connectPower = this.connectPower;
		signal.dataTypes = this.dataTypes;
		signal.terminalFault = this.terminalFault;
		signal.output12vStatus = this.output12vStatus;
		signal.arrearsStatus = this.arrearsStatus;
		signal.isHydOilThreshold = this.isHydOilThreshold;
		signal.isWaterAdding = this.isWaterAdding;
		signal.isWaterFull = this.isWaterFull;
		signal.isWasteDumping = this.isWasteDumping;
		signal.isSewageFull = this.isSewageFull;
		signal.isSewageEmission = this.isSewageEmission;
		signal.isBroomDown = this.isBroomDown;
		signal.isBroomDown2 = this.isBroomDown2;
		signal.isNozzleDown = this.isNozzleDown;
		signal.isWatering = this.isWatering;
		signal.isDredgorOpen = this.isDredgorOpen;
		signal.ptoStatus = this.ptoStatus;
		signal.isWaterNozzleOpen = this.isWaterNozzleOpen;
		signal.isWaterNozzleOpen2 = this.isWaterNozzleOpen2;
		signal.isWaterNozzleOpen3 = this.isWaterNozzleOpen3;
		signal.isLanceOpen = this.isLanceOpen;
		signal.isTrashDown = this.isTrashDown;
		signal.odorExc = this.odorExc;
		signal.oilExc = this.oilExc;
		signal.speedExc = this.speedExc;
		signal.hydOilExc = this.hydOilExc;
		signal.auxiMotorSpeedExc = this.auxiMotorSpeedExc;
		signal.waterNotFull = this.waterNotFull;
		signal.broomCanNotDown = this.broomCanNotDown;
		signal.broomSpeedExc = this.broomSpeedExc;
		signal.nozzleCanNotDown = this.nozzleCanNotDown;
		signal.broomOpenNoWatering = this.broomOpenNoWatering;
		signal.waterNozzleExc = this.waterNozzleExc;
		signal.motorSpeedExc = this.motorSpeedExc;
		signal.sewageEmission = this.sewageEmission;
		signal.sewageFull = this.sewageFull;
		signal.oilMutation = this.oilMutation;
		signal.trashNotDown = this.trashNotDown;
	}
	
	public void init(Data data){
		this.rawStatus = Long.parseLong(data.rawStatus, 16);
		this.rawSwitch = Long.parseLong(data.rawSwitch, 16);
		this.rawFault = Long.parseLong(data.rawFault, 16);
		this.rawOutput = Long.parseLong(data.rawOutput, 16);
		//状态位开始
		this.gpsStatus = indexOfByte(this.rawStatus,0);
		this.directionSN = indexOfByte(this.rawStatus,1);
		this.directionEW = indexOfByte(this.rawStatus,2);
		this.connectPower = indexOfByte(this.rawStatus, 3);
		this.dataTypes = indexOfByte(this.rawStatus,4);
		this.terminalFault = indexOfByte(this.rawStatus,5);
		this.output12vStatus = indexOfByte(this.rawStatus,6);
		this.arrearsStatus = indexOfByte(this.rawStatus,12);
		//开关位开始
		this.isHydOilThreshold = indexOfByte(this.rawSwitch,0);
		this.isWaterAdding = indexOfByte(this.rawSwitch,1);
		this.isWaterFull = indexOfByte(this.rawSwitch,2);
		this.isWasteDumping = indexOfByte(this.rawSwitch,3);
		this.isSewageFull = indexOfByte(this.rawSwitch,4);
		this.isSewageEmission = indexOfByte(this.rawSwitch,5);
		this.isBroomDown = indexOfByte(this.rawSwitch,6);
		this.isBroomDown2 = indexOfByte(this.rawSwitch,7);
		this.isNozzleDown = indexOfByte(this.rawSwitch,8);
		this.isWatering = indexOfByte(this.rawSwitch,9);
		this.isDredgorOpen = indexOfByte(this.rawSwitch,10);
		this.ptoStatus = indexOfByte(this.rawSwitch,11);
		this.isWaterNozzleOpen = indexOfByte(this.rawSwitch,12);
		this.isWaterNozzleOpen2 = indexOfByte(this.rawSwitch,13);
		this.isWaterNozzleOpen3 = indexOfByte(this.rawSwitch,14);
//		this.isLanceOpen = indexOfByte(this.rawSwitch,15);
		this.isTrashDown = indexOfByte(this.rawSwitch,16);
		//故障位开始
		this.odorExc = indexOfByte(this.rawFault,0);
		this.oilExc = indexOfByte(this.rawFault,1);
		this.speedExc = indexOfByte(this.rawFault,2); 	
		this.hydOilExc = indexOfByte(this.rawFault,3);
		this.auxiMotorSpeedExc = indexOfByte(this.rawFault,4);	
		this.waterNotFull = indexOfByte(this.rawFault,5);
		this.broomCanNotDown = indexOfByte(this.rawFault,6);
		this.broomSpeedExc = indexOfByte(this.rawFault,7);	
		this.nozzleCanNotDown = indexOfByte(this.rawFault,8);
		this.broomOpenNoWatering = indexOfByte(this.rawFault,9);
		this.waterNozzleExc = indexOfByte(this.rawFault,10);
		this.motorSpeedExc = indexOfByte(this.rawFault,12);
		this.sewageEmission = indexOfByte(this.rawFault,13);
		this.isLanceOpen = indexOfByte(this.rawFault,14);
		this.sewageFull = indexOfByte(this.rawFault,15);
		this.oilMutation = indexOfByte(this.rawFault,16);
		this.trashNotDown = indexOfByte(this.rawFault,17);
		//输出位开始
		this.output2 = indexOfByte(this.rawOutput,2);
		this.output3 = indexOfByte(this.rawOutput,3);
		if(data instanceof Data_1_x){
			//1.x版本 ACC 状态位 第3位
			this.accStatus = indexOfByte(this.rawStatus,3);
			//1.x版本 GPS天线  状态位 第5位
			this.gpsAerialStatus = indexOfByte(this.rawStatus,5);
			//1.x版本 设置  状态位 第7位
			this.settingStatus = indexOfByte(this.rawStatus,7);
			//1.x版本 输出0 输出位 第0位
			this.output0 = indexOfByte(this.rawOutput,0);
			//1.x版本 输出1 输出位 第1位
			this.output1 = indexOfByte(this.rawOutput,1);
		}else if(data instanceof Data_2_x){
			//2.x版本 电池 状态位 第3位
			this.batteryStatus = indexOfByte(this.rawStatus,3);
			//2.x版本 ACC 开关位 第17位
			this.accStatus = indexOfByte(this.rawSwitch,17);
			//2.x版本 GPS天线  开关位 第18位
			this.gpsAerialStatus = indexOfByte(this.rawSwitch,18);
			//2.x版本 设置  开关位 第19位
			this.settingStatus = indexOfByte(this.rawSwitch,19);
			//2.x版本 输出0 状态位 第8、9位
			this.output0 = indexOfByte(this.rawStatus,9)*2+indexOfByte(this.rawStatus,8);
			//2.x版本 输出1 状态位 第10、11位
			this.output1 = indexOfByte(this.rawStatus,11)*2+indexOfByte(this.rawStatus,10);
		}
		// (太仓) 左喷水 开关位3<-新增
		this.leftWateringStatus = indexOfByte(this.rawSwitch, 3);
		// (太仓) 右喷水 开关位8<-新增
		this.rightWateringStatus = indexOfByte(this.rawSwitch, 8);
	}
	/**
	 * 状态位
	 */
	private long rawStatus;
	/**
	 * 开关位
	 */
	private long rawSwitch;
	/**
	 * 故障位
	 */
	private long rawFault;
	/**
	 * 输出状态
	 */
	private long rawOutput;
	
	/**
	* GPS定位状态
	* 0：GPS未定位
	* 1：GPS已定位
	*/
	private int gpsStatus;
	
	/**
	* 当前方位
	* 0：南纬
	* 1：北纬
	*/
	private int directionSN;
	
	/**
	* 当前方位
	* 0：西经
	* 1：东经
	*/
	private int directionEW;
	
	/**
	 * 电源充电
	 * 0: 未接电源充电
	 * 1: 已接电源充电
	 */
	private int connectPower;
	
	/**
	* Acc状态
	*/
	private int accStatus;
	
	/**
	* 电源状态
	* 0：未接电源充电
	* 1：已接电源充电
	*/
	private int batteryStatus;
	
	/**
	* 数据类型
	* 0：历史数据
	* 1：实时数据
	*/
	private int dataTypes;
	/**
	 * 设备故障
	 * 0：GPS天线正常 1：GPS天线断开
	 */
	private int terminalFault;
	
	/**
	* GPS天线状态
	* 0：GPS天线正常
	* 1：GPS天线断开
	*/
	private int gpsAerialStatus;
	
	/**
	* 0：输出12V正常
	* 1：输出12V短路
	*/
	private int output12vStatus;
	
	/**
	* 0：正常状态
	* 1：设置状态
	*/
	private int settingStatus;
	
	/**
	* 使用状态
	* 0：正常
	* 1：欠费
	*/
	private int arrearsStatus;  
	
	/**
	* 液压油位是否低于预警值
	*/
	private int isHydOilThreshold;
	/**
	* 是否加水中
	*/
	private int isWaterAdding;
	/**
	* 水是否加满
	*/
	private int isWaterFull;
	/**
	* 是否垃圾倾倒中 
	*/
	private int isWasteDumping;
	
	/**
	*污水是否已满 
	*/
	private int isSewageFull;
	/**
	* 是否排污中
	*/
	private int isSewageEmission;
	/**
	* 扫盘是否放下
	*/
	private int isBroomDown;
	/**
	* 扫盘2是否放下
	*/
	private int isBroomDown2;
	/**
	* 吸嘴是否放下
	*/
	private int isNozzleDown;
	/**
	* 是否洒水中
	*/
	private int isWatering;
	/**
	*疏通传感器是否打开 
	*/
	private int isDredgorOpen;
	
	/**
	*取力器状态 
	*/
	private int ptoStatus;
	
	/**
	* 水流喷头1是否打开
	*/
	private int isWaterNozzleOpen;
	
	/**
	* 水流喷头2是否打开
	*/
	private int isWaterNozzleOpen2;
	
	/**
	* 水流喷头3是否打开
	*/
	private int isWaterNozzleOpen3;
	
	/**
	* 高压喷杆是否打开
	*/
	private int isLanceOpen;
	
	/**
	* 垃圾箱是否放下
	*/
	private int isTrashDown;

	/**
	* 臭味超标
	*/
	private int odorExc;
	/**
	* 油位低
	*/
	private int oilExc;
	/**
	* 工作时（见工作状态定义表）行驶超速
	*/
	private int speedExc; 	
	/**
	* 液压油位低
	*/
	private int hydOilExc;
	/**
	* 副发转速超标
	*/
	private int auxiMotorSpeedExc;	
	/**
	* 未加满水
	*/
	private int waterNotFull;
	/**
	* 扫盘不下落
	*/
	private int broomCanNotDown;
	/**
	* 扫盘转速异常
	*/
	private int broomSpeedExc;	
	/**
	* 吸嘴不下落
	*/
	private int nozzleCanNotDown;
	/**
	* 扫盘开启无洒水
	*/
	private int broomOpenNoWatering;
	/**
	* 水流传感器异常
	*/
	private int waterNozzleExc;
	
	/**
	* 发动机转速超速
	*/
	private int motorSpeedExc;
	/**
	* 污水排放
	*/
	private int sewageEmission;
	
	/**
	* 污水满
	*/
	private int sewageFull;
	
	/**
	* 油位突变报警 
	*/
	private int oilMutation;
	
	/**
	* 垃圾箱（填充箱）未放下
	*/
	private int trashNotDown;
	
	/**
	* 输出0
	* 0：正常
	* 1：开路
	* 2 短路
	*/
	private int output0;	  
	/**
	* 输出1
	* 0：正常
	* 1：开路
	* 2 短路
	*/
	private int output1;
	/**
	*输出状态 
	*/
	@SuppressWarnings("unused")
	private int output2;
	/**
	*输出状态 
	*/
	@SuppressWarnings("unused")
	private int output3;

	/**
	* 左喷头
	*/
	private int leftWateringStatus;
	
	/**
	* 右喷头
	*/
	private int rightWateringStatus;

	/**
	 * 加水状态
	 * @param device
	 * @param signal
	 */
	private int getWaterStatus(){
		int waterStatus = 0;
		if(this.isWaterAdding==1){
			waterStatus = 2;
		}else{
			waterStatus = 1;
		}
		if(this.isWaterFull==1){
			waterStatus += 4;
		}
		return waterStatus;
	}
	
	/**
	 * 洒水状态
	 */
	private int getWateringStatus(){
		int wateringStatus = 0;
		if(this.isWatering == 1 
				|| this.isDredgorOpen == 1
				|| this.ptoStatus == 1
				|| this.isWaterNozzleOpen == 1
				|| this.isWaterNozzleOpen2 == 1
				|| this.isWaterNozzleOpen3 == 1
				|| this.isLanceOpen == 1){
			wateringStatus = 1;
		}
		return wateringStatus;
	}
	
	private int indexOfByte(long val, int index) {
		if (((byte) (val >> index) & 0x01) > 0) {
			return 1;
		} else {
			return 0;
		}
	}
}
